package com.xianbeiying.app.ckgl.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lidroid.xutils.util.LogUtils;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.FaMenBean;
import com.xianbeiying.app.ckgl.data.Cmd;

import java.util.ArrayList;

/**
 * 卷帘数据适配
 */
public class RbFragmentAdapter  extends BaseAdapter{
    private Context context;
    private int length;
    private Handler handler;
//    private List<FaMenTypeBean.VBean.EQPSBean>  EQPS;
    private ArrayList<FaMenBean.VBean.STATUSBean> listFa;
    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public RbFragmentAdapter(Context context, ArrayList<FaMenBean.VBean.STATUSBean> listFa) {
        this.context = context;
        this.listFa = listFa;
    }

    @Override
    public int getCount() {
        return listFa.size();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_rb_item, parent, false);
            holder = new ViewHolder();
            holder.tv_rb=(TextView) convertView.findViewById(R.id.tv_rb);
            holder.rd_rb=(RadioGroup) convertView.findViewById(R.id.rd_rb);
            holder.rb_open=(RadioButton) convertView.findViewById(R.id.rb_open);
            holder.rb_middle=(RadioButton) convertView.findViewById(R.id.rb_middle);
            holder.rb_close=(RadioButton) convertView.findViewById(R.id.rb_close);
            holder.rb_last_line=(View) convertView.findViewById(R.id.rb_last_line);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
//        if(position==0){
//            holder.rb_open.setChecked(true);
   //     }
         holder.rb_last_line.setVisibility(View.VISIBLE);
        if((position+1)==length){
            holder.rb_last_line.setVisibility(View.GONE);
        }
        holder.tv_rb.setText(listFa.get(position).getNAME());
//        holder.rb_open.setChecked(false);
//        holder.rb_middle.setChecked(false);
//        holder.rb_close.setChecked(false);
        //上 打开
        if(listFa.get(position).getDEVSTATUS()== Cmd.EQP_ROLLING_STATUS_OPEN){
            LogUtils.e( Cmd.EQP_ROLLING_STATUS_OPEN+"===============");
            holder.rb_open.setChecked(true);
        }
        //停止
        if(listFa.get(position).getDEVSTATUS()==Cmd.EQP_ROLLING_STATUS_STOP){
            LogUtils.e( Cmd.EQP_ROLLING_STATUS_STOP+"===============");
            holder.rb_middle.setChecked(true);
        }
        //下 关闭
        if(listFa.get(position).getDEVSTATUS()==Cmd.EQP_ROLLING_STATUS_CLOSE){
            LogUtils.e( Cmd.EQP_ROLLING_STATUS_CLOSE+"===============");
            holder.rb_close.setChecked(true);
        }

        holder.rb_open.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Message msg=handler.obtainMessage();
                msg.what=R.id.rb_open;
                msg.arg1=position;
                handler.sendMessage(msg);
            }
        });
        holder.rb_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg=handler.obtainMessage();
                msg.what=R.id.rb_close;
                msg.arg1=position;
                handler.sendMessage(msg);
            }
        });
        holder.rb_middle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg=handler.obtainMessage();
                msg.what=R.id.rb_middle;
                msg.arg1=position;
                handler.sendMessage(msg);
            }
        });
        return convertView;
    }
    private class ViewHolder{
       TextView tv_rb;
       RadioGroup rd_rb;
       RadioButton rb_open;
       RadioButton rb_middle;
       RadioButton rb_close;
       View rb_last_line;
    }
}
